<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>动态平均数可视化</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background-color: #f8f9fa;
        }
        #chartContainer {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        .data-controls {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 10px;
            margin-bottom: 20px;
        }
        input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
        }
        button {
            background: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background: #45a049;
        }
    </style>
</head>
<body>
    <div id="chartContainer">
        <canvas id="myChart"></canvas>
    </div>
    
    <div class="data-controls">
        <input type="number" data-index="0" value="7">
        <input type="number" data-index="1" value="5">
        <input type="number" data-index="2" value="6">
        <input type="number" data-index="3" value="4">
        <input type="number" data-index="4" value="8">
    </div>
    <button onclick="updateChart()">更新图表</button>

    <script>
        // 初始数据设置
        let labels = ['1号', '2号', '3号', '4号', '5号'];
        let dataValues = [7, 5, 6, 4, 8];
        let averageLine = calculateAverage(dataValues);

        // 创建图表
        const ctx = document.getElementById('myChart').getContext('2d');
        const chart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: labels,
                datasets: [{
                    label: '数据值',
                    data: dataValues,
                    backgroundColor: 'rgba(54, 162, 235, 0.6)',
                    borderColor: 'rgba(54, 162, 235, 1)',
                    borderWidth: 1,
                    barPercentage: 0.6,
                    categoryPercentage: 0.8
                }, {
                    type: 'line',
                    label: '平均值',
                    data: Array(labels.length).fill(averageLine),
                    borderColor: '#ff6384',
                    borderWidth: 2,
                    pointRadius: 0,
                    borderDash: [5, 5]
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            stepSize: 2
                        }
                    }
                },
                plugins: {
                    legend: {
                        position: 'top'
                    }
                }
            }
        });

        // 计算平均值函数
        function calculateAverage(data) {
            return data.reduce((a, b) => a + b, 0) / data.length;
        }

        // 更新图表函数
        function updateChart() {
            const inputs = document.querySelectorAll('input');
            dataValues = Array.from(inputs).map(input => Number(input.value));
            averageLine = calculateAverage(dataValues);
            
            chart.data.datasets[0].data = dataValues;
            chart.data.datasets[1].data = Array(dataValues.length).fill(averageLine);
            chart.update();
        }

        // 实时更新监听
        document.querySelectorAll('input').forEach(input => {
            input.addEventListener('input', updateChart);
        });
    </script>
</body>
</html>